<!DOCTYPE html>
<html>
<head>

<link rel="stylesheet" type="text/css" href="https://www.ebi.ac.uk/~mnuhn/regulatory_build_stats/js/datatables.css"/>

<script src="https://www.ebi.ac.uk/~mnuhn/regulatory_build_stats/js/Chart.bundle.js"></script>
<script src="https://www.ebi.ac.uk/~mnuhn/regulatory_build_stats/js/utils.js"></script>

<script type="text/javascript" language="javascript" src="https://www.ebi.ac.uk/~mnuhn/regulatory_build_stats/js/jquery-1.12.4.js"></script>

<script type="text/javascript" src="https://www.ebi.ac.uk/~mnuhn/regulatory_build_stats/js/datatables.js"></script>

</head>
<body>

<h1 id="peak_calling">Peak calling</h1>

<p>
    This report is based 
</p>

<ul>
  <li>
    on the database <em>[% dbc.dbname %]</em> 
  </li>
  <li>
    on <em>[% dbc.host   %]:[% dbc.port   %]</em> 
  <li>
    on <em>[% time %]</em>.
  </li>
</ul>

<h2 id="failed_idr">Failed IDR</h2>

[% failed_idrs = idr_adaptor.fetch_all_failed %]

<p>
    The IDR failed in [% failed_idrs.size %] instances.
</p>

<script type="text/javascript" class="init">

$(document).ready(function() {
    $('#failed_idrs').DataTable(
        {
            "iDisplayLength": 5,
        }
    );

} );

</script>

<table id="failed_idrs" class="display" cellspacing="0" width="100%">
<thead>
    <tr>
        <th>Experiment</th>
        <th>Epigenome</th>
        <th>Type</th>
        <th>Error message</th>
    </tr>
</thead>
<tbody>

[% FOR failed_idr IN failed_idrs %]
<tr>
    <td>[% failed_idr.fetch_Experiment.name %]</td>
    <td>[% failed_idr.fetch_Experiment.epigenome.display_label %]</td>
    <td>[% failed_idr.type       %]</td>
    <td>[% failed_idr.failed_idr_pairs %]</td>
</tr>
[% END %]

</tbody>
</table>




<h2 id="failed_peak_callings">Failed peak callings</h2>

[% failed_peak_callings = peak_calling_adaptor.fetch_all_failed %]

<p>
    Peak calling failed in [% failed_peak_callings.size %] instances.
</p>

<script type="text/javascript" class="init">

$(document).ready(function() {
    $('#failed_peak_calls').DataTable();
} );

</script>

<table id="failed_peak_calls" class="display" cellspacing="0" width="100%">
<thead>
    <tr>
        <th>Name</th>
        <th>Feature type</th>
        <th>Broad peaks</th>
        <th>Epigenome</th>
        <th>Error message</th>
    </tr>
</thead>
<tbody>

[% FOR failed_peak_calling IN failed_peak_callings %]
<tr>
    <td>[% failed_peak_calling.name %]</td>
    <td>[% failed_peak_calling.fetch_FeatureType.name       %]</td>
    <td>[% boolean_to_yes_no( failed_peak_calling.fetch_FeatureType._creates_broad_peaks ) %]</td>
    <td>[% failed_peak_calling.fetch_Epigenome.display_label %]</td>
    <td>[% failed_peak_calling.error_message%]</td>
</tr>
[% END %]

</tbody>
</table>


<h2 id="prop_of_genome_in_peaks">Proportion of genome in peaks by feature type and epigenome in percent (0-100)</h2>

[%- FOR current_feature_type IN feature_types -%]

<h3>[% current_feature_type.name %]</h3>

<p>If the [% current_feature_type.name %] peaks from all epigenomes were projected to the genome, they would cover
    [% 
        round_percent(
          peak_calling_statistic_adaptor
            .fetch_coverage_percent_by_FeatureType(
                current_feature_type
            ).value
        )
    %]% of the genome.
</p>

    <div id="container_[% current_feature_type.name %]" style="width: 60%;">
        <canvas id="canvas_[% current_feature_type.name %]"></canvas>
    </div>
    <script>
        var color = Chart.helpers.color;
        
        var selected_colors = [
            'rgb(255, 99, 132)',
            'rgb(255, 159, 64)',
            'rgb(255, 205, 86)',
            'rgb(75, 192, 192)',
            'rgb(54, 162, 235)',
            'rgb(153, 102, 255)',
            'rgb(201, 203, 207)',
            'rgb(0,0,139)',
            'rgb(75,0,130)',
            'rgb(0,128,128)',
            'rgb(128,0,128)',
        ];

        var barChartData_[% current_feature_type.name %] = {
            labels: [
                [% FOR epigenome IN epigenomes %]
                    '[% epigenome.display_label %]',
                [%- END %]
            ],
            datasets: [
                {
                    label: '[% current_feature_type.name %]',
                    backgroundColor: color(selected_colors[ [% loop.count %] ]).alpha(0.5).rgbString(),
                    borderWidth: 1,
                    data: [
                        [% FOR current_epigenome IN epigenomes %]
                            [% peak_callings = peak_calling_adaptor.fetch_all_by_Epigenome_FeatureType(current_epigenome, current_feature_type) %]
                            [% IF peak_callings.size > 0 %]
                                [% FOR peak_calling IN peak_callings  -%]
                                    '[% 
                                    round_percent(
                                        length_to_percent(
                                            peak_calling_statistic_adaptor
                                                .fetch_by_PeakCalling_total_length(
                                                    peak_calling, 
                                                    'total_length'
                                                ).value
                                        )
                                    )
                                    %]',
                                [%- END %]
                            [% ELSE %]
                                '0',
                            [% END %]
                        [% END %]
                    ]
                },
            ]
        };
    </script>
    
[% END %]

    <script>
        window.onload = function() {
        
            [%- FOR current_feature_type IN feature_types -%]
            
            var ctx_[% current_feature_type.name %] = document.getElementById('canvas_[% current_feature_type.name %]').getContext('2d');
            
            window.myBar = new Chart(ctx_[% current_feature_type.name %], {
                type: 'bar',
                data: barChartData_[% current_feature_type.name %],
                options: {
                    responsive: true,
                    legend: {
                        position: 'top',
                        display: false,
                    },
                    scales: {
                        yAxes: [{
                            ticks: {
                                min: 0
                            }
                        }]
                    },
                    title: {
                        display: false,
                        text: 'Total peak lengths in [% current_feature_type.name %]'
                    }
                }
            });
            
            [% END %]
        };
    </script>

<h2>Peak calling statistics</h2>

<p>
  To compute the proportion of genome in peaks, a genome length of [% format_number( genome_size_in_bp ) %] base pairs was used.
</p>

<script type="text/javascript" class="init">

$(document).ready(function() {
    $('#peak_calling_1').DataTable();
} );

$(document).ready(function() {
    $('#read_file_3').DataTable(
        {
            "iDisplayLength": 10,
        }
    );
} );

$(document).ready(function() {
    $('#read_file_4').DataTable(
        {
            "iDisplayLength": 10,
        }
    );
} );

</script>

<table id="peak_calling_1" class="display" cellspacing="0" width="100%">
<thead>
    <tr>
        <th>Epigenome</th>
        <th>Feature type</th>
        <th>Proportion of genome in peaks</th>
        <th>Number of peaks</th>
        <th>Average length</th>
    </tr>
</thead>
<tbody>

[% FOR peak_calling IN peak_calling_adaptor.fetch_all %]
<tr>
    <td>[% peak_calling.fetch_Epigenome.display_label %]</td>
    <td>[% peak_calling.fetch_FeatureType.name        %]</td>
    <td align="right">
        [% 
        round_percent(
            length_to_percent(
                peak_calling_statistic_adaptor
                    .fetch_by_PeakCalling_total_length(
                        peak_calling, 
                        'total_length'
                    ).value
            )
        )
        %]
    </td>
    <td align="right">
        [% 
        format_number(
            peak_calling_statistic_adaptor
                .fetch_by_PeakCalling_total_length(
                    peak_calling,
                    'num_peaks'
                ).value
        )
        %]
    </td>
    <td align="right">
        [% 
        format_number(
            peak_calling_statistic_adaptor
                .fetch_by_PeakCalling_total_length(
                    peak_calling,
                    'average_length'
                ).value
        )
        %]
    </td>
</tr>
[% END %]

</tbody>
</table>

<h2>IDR</h2>

<h3>Signal Experiments</h3>

<table id="read_file_3" class="display" cellspacing="0" width="100%">
<thead>
    <tr>
        <th>Experiment</th>
        <th>Feature type</th>
        <th>Read type</th>
        <th>Replicate configurations</th>
        <th>Number of reads</th>
        <th>Sum of read file sizes in GB</th>
        <th>Ratio of read counts: signal/control</th>
        <th>IDR type</th>
        <th>Max peaks</th>
        <th>Failed IDR pairs</th>
    </tr>
</thead>
<tbody>

[% FOR experiment IN signal_experiments %]

[% idr = fetch_idr(experiment) %]

<tr>
    <td>[% experiment.name %]</td>
    <td align="right">[% experiment.feature_type.name       %]</td>
    <td>[% experiment.summarise_read_types %]</td>
    <td>[% experiment.summarise_replicate_configurations %]</td>
    <td align="right">[% format_number( experiment.sum_number_of_reads )   %]</td>
    <td align="right">[% format_number( bytes_to_gb ( experiment.sum_read_file_sizes ) )   %]</td>
    <td align="right">[% round_num( experiment.signal_to_control_read_file_ratio ) %]</td>
    <td>[% idr.type %]</td>
    <td align="right">[% format_number( idr.max_peaks ) %]</td>
    <td>[% idr.failed_idr_pairs %]</td>
</tr>
[% END %]

</tbody>
</table>

<h3>Control Experiments</h3>

<table id="read_file_4" class="display" cellspacing="0" width="100%">
<thead>
    <tr>
        <th>Experiment</th>
        <th>Feature type</th>
        <th>Read type</th>
        <th>Replicate configurations</th>
        <th>Number of reads</th>
        <th>Sum of read file sizes in GB</th>
    </tr>
</thead>
<tbody>

[% FOR experiment IN control_experiments %]
<tr>
    <td>[% experiment.name %]</td>
    <td>[% experiment.feature_type.name       %]</td>
    <td>[% experiment.summarise_read_types %]</td>
    <td>[% experiment.summarise_replicate_configurations %]</td>
    <td align="right">[% format_number( experiment.sum_number_of_reads )   %]</td>
    <td align="right">[% format_number( bytes_to_gb ( experiment.sum_read_file_sizes ) )   %]</td>
</tr>
[% END %]

</tbody>
</table>

</body>
</html>
